// pages/personage/personage.js
const app = getApp()
const util = require('../../utils/util.js')
import { UserModel } from '../../models/user.js'
let user = new UserModel()
Page({
  data: {
    title: "个人中心", //当前页面的标题

    loginshow: false
  },
  onLoad: function(options) {
    this.setData({
      headerheight: app.globalData.navHeight,
    })
  },

  onShow: function() {
    wx.getSetting({
      success: res => {
        if (!res.authSetting['scope.userInfo']) {
          this.setData({
            loginshow: true
          })
        } else {
          this.setData({
            loginshow: false
          })
        }
      }
    })     

    if (wx.getStorageSync("userInfo").userName == "") {
      this.setData({
        userUrl: "/images/touxiang.png", //头像
        userName: "未登录",
      })
    } else {
      this.setData({
        userUrl: wx.getStorageSync("userInfo").userImgUrl,
        userName: wx.getStorageSync("userInfo").userName,
      })
    }
    this.request()
  },

  login: function() {
    var that = this
    wx.login({
      success: function(res) {
        var code = res.code
        var data = {
          userCode: code,
          userImageUrl: "",
          userName: ""
        }
        util.promotion(app.globalData.appurl + "/qinqiang/user", "POST", data, that.succeed)
      }
    })
  },
  succeed: function(res) {
    wx.setStorageSync('tekon', res.data.token)
    wx.setStorageSync('userInfo', res.data.user)
    this.request()
  },
  request: function() {
    wx.showNavigationBarLoading()
    wx.showLoading({
      title: "玩命加载中...",
    })
    var data = {
      user_id: wx.getStorageSync("userInfo").id,
      pageIndex: 1,
      pageSize: 20,
    }
    var header = {
      'authorization': wx.getStorageSync("tekon"),
      'content-type': "application/x-www-form-urlencoded"
    }
    //        总金币数量
    util.formhttp(app.globalData.appurl + "/goldconins/mycount", 'get', data, header, this.stat);
    //获取 观看时长
    util.formhttp(app.globalData.appurl + '/mission/watchTime', 'get', '', header, this.watchTimedata);
  },
  stat: function(res) {
    console.log(res)
    if (res.status == 401) {
      this.login()
    }
    wx.hideNavigationBarLoading()
    wx.hideLoading()
    this.setData({
      money: res.data.coins,
        times: res.data.todayVideos,
      payStatus: res.data.payStatus
    })
  },
  watchTimedata: function(res) {
    console.log(res)
   
    this.setData({
//       times: res.data.watchTimes,
      friend: res.data.promoteNum
    })
  },
  uploading: function() {
    this.setData({
      kefushow: true
    })
  },
  task: function() {
    wx.switchTab({
      url: '/pages/task/task',
    })
  }
})